package main

import (
	"encoding/json"
	"fmt"
	"github.com/goinggo/mapstructure"
	"github.com/unidoc/unioffice/common/license"
	"github.com/unidoc/unioffice/spreadsheet"
	"io/ioutil"
	"log"
	"reflect"
)

const licenseKey = `
-----BEGIN UNIDOC LICENSE KEY-----
Free trial license keys are available at: https://unidoc.io/
-----END UNIDOC LICENSE KEY-----
`

func init() {
	err := license.SetLicenseKey(licenseKey, `Company Name`)
	if err != nil {
		panic(err)
	}
}
type Per struct {
	Name string
	Hobby string
	Addr struct{
		Address string
		Tel int64
	}
}
//struct嵌套struct
type Animal struct {
	No string
	Results []struct {
		Name  string
		Order string
	}
}

func json2Sturct()  {
	//1.创建json数据
	b := []byte(`{"Name":"zhangsan","Hobby":"女","Addr":{"Address":"xian","Tel":15291967131}}`)
	//2.声明空interface
	//var i interface{}
	per := Per{}
	//3.解析
	err := json.Unmarshal(b, &per)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(per)

	//json转struct
	//	var jsonBlob = []byte(`{"No":"001","Results":[
	//	{"Name": "Platypus", "Order": "Monotremata","id":111},
	//	{"Name": "Quoll",    "Order": "Dasyuromorphia"}
	//]}`)
	//
	//	var animals Animal
	//	errs := json.Unmarshal(jsonBlob, &animals)
	//	if errs != nil {
	//		fmt.Println("error:", errs)
	//	}
	//	fmt.Printf("%+v", animals)

	//默认转成了map
	//fmt.Println(i)
	//fmt.Println(reflect.ValueOf(i))
	//fmt.Println(reflect.TypeOf(i))
	//s := reflect.ValueOf(i)
	//for i := 0; i < s.Len(); i++ {
	//	i2 := s.Index(i).Interface().(map[string]interface{})
	//	fmt.Println(i2["Name"])
	//}
}

var lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin lobortis, lectus dictum feugiat tempus, sem neque finibus enim, sed eleifend sem nunc ac diam. Vestibulum tempus sagittis elementum`

func main()  {
	ss := spreadsheet.New()
	defer ss.Close()
	// add a single sheet
	sheet := ss.AddSheet()

	// rows
	for r := 0; r < 5; r++ {
		row := sheet.AddRow()
		// and cells
		for c := 0; c < 5; c++ {
			cell := row.AddCell()
			cell.SetString(fmt.Sprintf("row %d cell %d", r, c))
		}
	}

	if err := ss.Validate(); err != nil {
		log.Fatalf("error validating sheet: %s", err)
	}

	ss.SaveToFile("simple.xlsx")
	//r:=gin.Default()
	//r.GET("/ping", func(c *gin.Context) {
	//
	//	c.JSON(200, gin.H{
	//		"message": "pong",
	//	})
	//})
	//r.Run()
}

func getTableStruct()  []TableStruct {
	var tableSlice  []TableStruct
	data, err := ioutil.ReadFile("e://sjxt")
	if err != nil {
		fmt.Println("File reading error", err)
		return nil
	}
	var dat interface{}
	if err := json.Unmarshal([]byte(string(data)), &dat); err == nil {
		d := reflect.ValueOf(dat)
		for i := 0; i < d.Len(); i++ {
			map_type := d.Index(i).Interface().(map[string]interface{})
			if map_type["type"]=="model" {
				var mapp = make(map[string]interface{})
				resources := reflect.ValueOf(map_type["data"]).Interface().(map[string]interface{})
				res :=reflect.ValueOf(resources["resources"].([]interface {})[0]).Interface().(map[string]interface{})
				mapp["TableName"]=fmt.Sprintf("%v",res["table"])
				fields := reflect.ValueOf(res["fields"]).Interface().([]interface{})
				model := reflect.ValueOf(resources["model"]).Interface().(map[string]interface{})
				mapp["Name"]=fmt.Sprintf("%v",model["name"])
				propertyDetails :=reflect.ValueOf(model["propertyDetails"]).Interface().([]interface{})
				var slice  []map[string]interface{}
				for _,value := range propertyDetails{
					mm := make(map[string]interface{})
					val:=reflect.ValueOf(value).Interface().(map[string]interface{})
					mm["caption"]=val["caption"]
					mappingInfoDetails:=val["mappingInfoDetails"]
					fieldName:=reflect.ValueOf(reflect.ValueOf(reflect.ValueOf(mappingInfoDetails).Interface().(map[string]interface{})["fieldMappings"]).Interface().([]interface{})[0]).Interface().(map[string]interface{})["fieldName"]
					mm["fieldName"]=fieldName
					for _,field := range fields{
						inter :=reflect.ValueOf(field).Interface()
						if inter.(map[string]interface{})["name"]== fieldName{
							mm["dataType"]=inter.(map[string]interface{})["type"]
						}
					}
					slice= append(slice, mm)
				}
				mapp["FieldStruct"]=slice
				var tableStruct TableStruct
				err := mapstructure.Decode(mapp, &tableStruct)
				if err != nil {
					fmt.Println(err)
				}
				tableSlice=append(tableSlice,tableStruct)

			}
		}
	}else {
		fmt.Println(err)
	}
  return tableSlice
}

type TableStruct struct {
	Name string
	TableName string
	FieldStruct [] struct {
	FieldName string
	Caption string
	DataType string
	}
}
type  CreateWord struct {

}